﻿@using Orchard.Utility.Extensions
@model System.Collections.IEnumerable
@{
    //http://haacked.com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx
    var tableModel =Model.Cast<object>().ToArray();
    Func<ModelMetadata,bool> shouldShow= mm=>mm.ShowForDisplay && !mm.IsComplexType && !ViewData.TemplateInfo.Visited(mm);

    ModelMetadata[]properties=null;
    if(tableModel.Any()) {
        properties = ModelMetadata.FromLambdaExpression(m => tableModel[0], ViewData)
            .Properties
            .Where(shouldShow).ToArray();
    }
}
Table template
<table class="tableDisplayTemplate">
    <thead>
        <tr>
            @foreach(var p in properties.Maybe()) {
                <th>@p.GetDisplayName()</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var tm in tableModel.Select((m, i) => new { m, i }))
        {
            var itemMd = ModelMetadata.FromLambdaExpression(m => tableModel[tm.i], ViewData);
            <tr>
                @foreach(var p in properties.Maybe()) {
                    var propertyMetadata = itemMd.Properties.Single(m => m.PropertyName == p.PropertyName);
                    <td>@Html.DisplayFor(m=>propertyMetadata.Model)</td>
                }
            </tr>
        }
    </tbody>
</table>

    
    

